package Eyjafjalla.action;

import Eyjafjalla.helper.EyjafjallaHelper;
import Eyjafjalla.helper.StringHelper;
import com.megacrit.cardcrawl.actions.AbstractGameAction;
import com.megacrit.cardcrawl.cards.AbstractCard;
import com.megacrit.cardcrawl.core.Settings;
import com.megacrit.cardcrawl.dungeons.AbstractDungeon;

import java.util.ArrayList;

public class PracticalAction extends AbstractGameAction {
    public PracticalAction(boolean isFire){
        actionType = ActionType.CARD_MANIPULATION;
        this.isFire = isFire;
        this.startDuration = duration = Settings.ACTION_DUR_FAST;
    }

    @Override
    public void update() {
        if(duration == startDuration){
            if(AbstractDungeon.getMonsters().areMonstersBasicallyDead()){
                this.isDone = true;
                return;
            }
            ArrayList<AbstractCard> cardsToDiscard = new ArrayList<>();
            ArrayList<AbstractCard> cardsToProject = new ArrayList<>();
            for(AbstractCard c: AbstractDungeon.player.hand.group){
                if(c.costForTurn>=0&&c.costForTurn%2==0)
                    cardsToDiscard.add(c);
                else
                    cardsToProject.add(c);
            }
            for(AbstractCard c:cardsToDiscard){
                c.unhover();
                AbstractDungeon.player.hand.moveToDiscardPile(c);
                c.triggerOnManualDiscard();
            }
            for(AbstractCard c:cardsToProject){
                if(AbstractDungeon.player.hand.size()<10){
                    AbstractCard copy = c.makeStatEquivalentCopy();
                    copy.untip();
                    copy.current_x = c.current_x;
                    copy.current_y = c.current_y;
                    copy.angle = c.angle;
                    copy.drawScale = c.drawScale;
                    StringHelper.addProject(copy);
                    if(isFire){
                        EyjafjallaHelper.markCardToFire(copy,1);
                    }
                    copy.flash();
                    copy.triggerWhenCopied();
                    int index = AbstractDungeon.player.hand.group.indexOf(c);
                    AbstractDungeon.player.hand.group.add(index+1,copy);
                }
                else
                    break;
            }
            AbstractDungeon.player.hand.refreshHandLayout();
            AbstractDungeon.player.hand.applyPowers();
        }

        this.tickDuration();
    }

    boolean isFire;
}
